package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollUtil;
import com.morningstar.common.result.Result;
import com.morningstar.system.config.MinioConfig;
import com.morningstar.system.controller.result.ArcoDesignFile;
import com.morningstar.system.controller.result.FileUploadResult;
import com.morningstar.system.dao.entity.SysFile;
import com.morningstar.system.service.MinioService;
import com.morningstar.system.service.SysFileService;
import io.minio.messages.Bucket;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/system/file")
public class SysFileController {

    @Resource
    private MinioConfig minioConfig;

    @Resource
    private MinioService minioService;

    @Resource
    private SysFileService service;


    @GetMapping("/bucketExists")
    public Result<Boolean> bucketExists(@RequestParam("bucketName") String bucketName) {
        return Result.successData(minioService.bucketExists(bucketName));
    }


    @GetMapping("/makeBucket")
    public Result<Boolean> makeBucket(String bucketName) {
        return Result.successData(minioService.makeBucket(bucketName));
    }


    @GetMapping("/removeBucket")
    public Result<Boolean> removeBucket(String bucketName) {
        return Result.successData(minioService.removeBucket(bucketName));
    }


    @GetMapping("/getAllBuckets")
    public Result<List<Bucket>> getAllBuckets() {
        List<Bucket> allBuckets = minioService.getAllBuckets();
        return Result.successData(allBuckets);
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 文件上传结果
     * @throws Exception 异常信息
     */
    @PostMapping("/upload")
    @SaCheckLogin
    public Result<FileUploadResult> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return Result.successData(service.uploadFile(file));
    }

    /**
     * 根据文件Id获取文件信息  arco design 文件列表
     *
     * @param fileIds 文件Id
     * @return 文件信息
     */
    @PostMapping("/list/arco")
    @SaCheckLogin
    public Result<List<ArcoDesignFile>> listArco(@RequestBody List<Long> fileIds) {
        if (CollUtil.isEmpty(fileIds)) {
            return Result.successData(CollUtil.newArrayList());
        }
        String urlPrefix = service.getUrlPrefix();
        List<SysFile> sysFiles = service.listFile(fileIds);
        List<ArcoDesignFile> result = sysFiles.stream().map(sysFile -> ArcoDesignFile.success(sysFile, urlPrefix)).toList();
        return Result.successData(result);
    }

    @GetMapping("/preview")
    @SaCheckLogin
    public Result<String> preview(@RequestParam("fileName") String fileName) {
        return Result.successData(minioService.preview(fileName));
    }


    @GetMapping("/download")
    @SaCheckLogin
    public Result<Object> download(@RequestParam("fileName") String fileName, HttpServletResponse res) {
        minioService.download(fileName, res);
        return Result.successNoData();
    }


    @PostMapping("/delete")
    @SaCheckLogin
    public Result<String> remove(String url) throws Exception {
        String objName = url.substring(url.lastIndexOf(minioConfig.getBucketName() + "/") + minioConfig.getBucketName().length() + 1);
        minioService.remove(objName);
        return Result.successData(objName);
    }

}
